package com.wangzhen.exchangesTwo.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wz
 * @company 公司
 * @create 2020-04-14 15:08
 */
@Configuration
public class RabbitMQConfig {
    //业务交换机
    public static final String BUSINESS_EXCHANGE_NAME = "business_exchange_name";
    //实时数据队列
    public static final String BUSINESS_REALTIEMMSG_NAME = "business_realtiemmsg_name";
    //其余数据队列
    public static final String BUSINESS_OTHERMSG_NAME = "business_othermsg_name";
    //死信交换机
    public static final String DEAD_LETTER_EXCHANGE = "dead_letter_exchange";
    //死信routing-key
    public static final String DEAD_LETTER_REALTIMEMSG_ROUTING_KEY = "dead_letter_realtimemsg_routing_key";
    //死信routing-key
    public static final String DEAD_LETTER_OTHERMSG_ROUTING_KEY = "dead_letter_othermsg_routing_key";
    //死信队列A
    public static final String DEAD_LETTER_REALTIMEMSG_NAME = "dead_letter_realtimemsg_name";
    //死信队列B
    public static final String DEAD_LETTER_OTHERMSG_NAME = "dead_letter_othermsg_name";
    //业务失败延迟交换机
    public static final String BUSINESS_DELAY_EXCHANGE_NAME = "business_delay_exchange_name";
    //业务失败延迟队列A
    public static final String BUSINESS_DELAY_REALTIMEMSG_NAME = "business_delay_realtimemsg_name";
    //业务失败延迟队列B
    public static final String BUSINESS_DELAY_OTHERMSG_NAME = "business_delay_othermsg_name";

    // 声明业务Exchange
    @Bean("businessExchange")
    public TopicExchange businessExchange() {
        return new TopicExchange(BUSINESS_EXCHANGE_NAME);
    }

    // 声明死信Exchange
    @Bean("deadLetterExchange")
    public DirectExchange deadLetterExchange() {
        return new DirectExchange(DEAD_LETTER_EXCHANGE);
    }

    //声明业务延迟交换机
    @Bean("delayBusinessExchange")
    public TopicExchange delayBusinessExchange() {
        return new TopicExchange(BUSINESS_DELAY_EXCHANGE_NAME);
    }

    // 声明业务队列A
    @Bean("businessQueueA")
    public Queue businessQueueA() {
        Map<String, Object> args = new HashMap<>(2);
        return QueueBuilder.durable(BUSINESS_REALTIEMMSG_NAME).withArguments(args).build();
    }

    // 声明业务队列B
    @Bean("businessQueueB")
    public Queue businessQueueB() {
        Map<String, Object> args = new HashMap<>(2);
        return QueueBuilder.durable(BUSINESS_OTHERMSG_NAME).withArguments(args).build();
    }

    // 声明业务延时队列A
    @Bean("delayBusinessQueueA")
    public Queue delayBusinessQueueA() {
        Map<String, Object> args = new HashMap<>(3);
        args.put("x-message-ttl", 10 * 1000);
        //       x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
//       x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", DEAD_LETTER_REALTIMEMSG_ROUTING_KEY);
        return QueueBuilder.durable(BUSINESS_DELAY_REALTIMEMSG_NAME).withArguments(args).build();
    }

    // 声明业务延时队列B
    @Bean("delayBusinessQueueB")
    public Queue delayBusinessQueueB() {
        Map<String, Object> args = new HashMap<>(3);
        args.put("x-message-ttl", 10 * 1000);
        //       x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
//       x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", DEAD_LETTER_OTHERMSG_ROUTING_KEY);
        return QueueBuilder.durable(BUSINESS_DELAY_OTHERMSG_NAME).withArguments(args).build();
    }

    // 声明死信队列A
    @Bean("deadLetterQueueA")
    public Queue deadLetterQueueA() {
        Map<String, Object> args = new HashMap<>(1);
        args.put("x-message-ttl", 10 * 1000);

        return QueueBuilder.durable(DEAD_LETTER_REALTIMEMSG_NAME).withArguments(args).build();
    }

    // 声明死信队列B
    @Bean("deadLetterQueueB")
    public Queue deadLetterQueueB() {

        Map<String, Object> args = new HashMap<>(1);
        //过期时间
        args.put("x-message-ttl", 10 * 1000);

        return QueueBuilder.durable(DEAD_LETTER_OTHERMSG_NAME).withArguments(args).build();
    }

    // 声明业务队列A绑定关系
    @Bean
    public Binding businessBindingA(@Qualifier("businessQueueA") Queue queue,
                                    @Qualifier("businessExchange") TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("iov.realtimemsg");
    }

    // 声明业务队列B绑定关系
    @Bean
    public Binding businessBindingB(@Qualifier("businessQueueB") Queue queue,
                                    @Qualifier("businessExchange") TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("iov.othermsg");
    }


    // 声明业务延迟队列A绑定关系
    @Bean
    public Binding delayBusinessBindingA(@Qualifier("delayBusinessQueueA") Queue queue,
                                         @Qualifier("delayBusinessExchange") TopicExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("iov.delay.realtimemsg");
    }

    // 声明业务延迟队列B绑定关系
    @Bean
    public Binding delayBusinessBindingB(@Qualifier("delayBusinessQueueB") Queue queue,
                                         @Qualifier("delayBusinessExchange") TopicExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("iov.delay.othermsg");
    }

    // 声明死信队列A绑定关系
    @Bean
    public Binding deadLetterBindingA(@Qualifier("deadLetterQueueA") Queue queue,
                                      @Qualifier("deadLetterExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_REALTIMEMSG_ROUTING_KEY);
    }

    // 声明死信队列B绑定关系
    @Bean
    public Binding deadLetterBindingB(@Qualifier("deadLetterQueueB") Queue queue,
                                      @Qualifier("deadLetterExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_OTHERMSG_ROUTING_KEY);
    }

}
